﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebFunctions;
using HtmlAgilityPack;
using ExcelWrapper;
using System.Data;
using System.Windows.Forms;

namespace AutoIndustryFiles
{
    public class FileLink
    {
        public FileLink()
        {

        }
        public FileLink(string id, string url, string name, string categoryPath, bool isFileLink, string fileType="", string releaseDate="",string description="")
        {
            Id = id;
            Url = url;
            Name = name;
            CategoryPath = categoryPath;
            IsFileLink = isFileLink;
            FileType = fileType;
            ReleaseDate = releaseDate;
            Description = description;
        }
        /// <summary>
        /// 链接ID，可以自编或使用网站自带的ID
        /// </summary>
        public string Id;
        /// <summary>
        /// 链接URL
        /// </summary>
        public string Url;
        /// <summary>
        /// 链接名称，一般为链接对应A标签的innertext
        /// </summary>
        public string Name;
        /// <summary>
        /// 栏目层次: 一级link名称->二级link名称->...
        /// </summary>
        public string CategoryPath;
        public string Description;
        /// <summary>
        /// 是否文件链接
        /// </summary>
        public bool IsFileLink;
        /// <summary>
        /// 文件类型，可表达为物理文件类型或资料分类
        /// </summary>
        public string FileType;
        /// <summary>
        /// 发布或修改日期
        /// </summary>
        public string ReleaseDate;
    }

    public class FileLinksScraper2
    {
        //主页URL
        public string MainPage = "";

        //用于第一个sheet的内容
        //厂商名称
        public string CompanyName = string.Empty;
        //栏目名称
        public string ColumnName = string.Empty;
        //栏目地址
        public string CompanyUrl = string.Empty;

        public List<FileLink> FileLinks = new List<FileLink> { };

        //待扫描的链接
        public Queue<FileLink> FileLinksToScrape = new Queue<FileLink> { };

        public delegate void OnProcessing(string className, string msg, ref bool bCancel);
        public event OnProcessing OnProcessingEvent = null;

        public delegate void OnNoFound(HtmlAgilityPack.HtmlDocument doc, ref FileLink lnkParent);
        public delegate void ExtractAttributes(HtmlAgilityPack.HtmlNode node, ref FileLink lnk);

        public virtual void GenerateAllLinks(string param)
        {
            return;
        }

        /// <summary>
        /// 通用方法：产生下一级链接
        /// </summary>
        /// <param name="doc">网页html对象</param>
        /// <param name="lnkParent">上一级link</param>
        /// <param name="xPath">查找下一级的xpath</param>
        /// <param name="extractMethod">产生link属性的自定义方法</param>
        /// <param name="checkMethod">判断是否为文件的自定义方法</param>
        public virtual void GenerateSubWebLinks(HtmlAgilityPack.HtmlDocument doc, ref FileLink lnkParent,             
            string xPath,
            OnNoFound OnNoFoundMethod = null,
            ExtractAttributes extractMethod = null            
            )
        {
            //对每个符合xpathbase的节点
            HtmlNodeCollection hnc = doc.DocumentNode.SelectNodes(xPath);

            if (hnc == null)
            {
                if (OnNoFoundMethod != null)
                    OnNoFoundMethod(doc,ref lnkParent);
                else
                    lnkParent.IsFileLink = true;
                return;
            }
            foreach (HtmlAgilityPack.HtmlNode node in hnc)
            {
                FileLink lnk = new FileLink();

                lnk.Id = FileLinks.Count.ToString();//内部ID,可能重复?
                lnk.Name = node.InnerText;
                lnk.Url = node.GetAttributeValue("href", "").ToString();
                lnk.IsFileLink = false;

                //修订属性的方法，可修改Id,Name,Url,Filetype,ReleaseDate属性
                if (extractMethod != null)
                    extractMethod(node,ref lnk);

                if (!string.IsNullOrEmpty(lnk.Id))
                {

                    //网页相对路径改为全路径 
                    lnk.Url = FormatUrl(lnk.Url, lnkParent.Url);
                    lnk.CategoryPath = lnkParent.CategoryPath + "->" + lnk.Name;

                    FileLinks.Add(lnk);
                    FileLinksToScrape.Enqueue(lnk);
                }
                else
                {
                    //lnk不是有效链接 不做处理
                    
                }
            }
        }

        protected void UpdateMsg(string className, string msg, ref bool bCancel)
        {
            if (OnProcessingEvent != null)
                OnProcessingEvent(className, msg, ref bCancel);
        }

        public static String FormatUrl(String urlRelative, string urlAbsolute)
        {

            Uri baseUri = new Uri(urlAbsolute); // http://www.enet.com.cn/enews/inforcenter/designmore.jsp
            Uri absoluteUri = new Uri(baseUri, urlRelative);//相对绝对路径都在这里转 这里的urlx ="../test.html"
            return absoluteUri.ToString();//   http://www.enet.com.cn/enews/test.html   
        }

        public virtual void ExportToExcel(ExcelApp ea, string path, string template)
        {
            bool f = false;
            UpdateMsg(this.GetType().Name, "正在导出到EXCEL到" + path, ref f);

            ea.OpenFile(template);

            //第一个sheet
            ea.SetCurrentSheet(1);
            ea.SetCellValue("A2", CompanyName);
            ea.SetCellValue("B2", ColumnName);
            ea.SetCellValue("C2", MainPage);

            //第二个sheet，显示栏目树型结构
            /*ea.SetCurrentSheet(2);

            IEnumerable<WebLink> wls = WebLinks.Where(v1 => v1.LinkLevel == 1).OrderBy(v1 => v1.LinkId);

            int row = 1;
            ColumnHierachy(ea, wls, ref row, 1);
            /*var q = from v1 in WebLinks.Where(s=>s.LinkLevel==1)
                    from v2 in WebLinks.Where(s=>s.LinkLevel==2)
                    where v1.LinkId == v2.ParentLinkId 
                    orderby v1.LinkId ascending, v2.LinkId ascending
                    select new {linkid = v2.LinkId.ToString(),n1 = v1.LinkName,n2 = v2.LinkName};

            int k = 1;
            var item_bak = q.ElementAt(0);
            foreach(var item in q)
            {
                string name1 = item.n1, name2 = item.n2;
                if (k > 1)
                {
                    if (item.n1 == item_bak.n1)
                        name1 = "";
                    if (item.n2 == item_bak.n2)
                        name2 = "";
                }
                ea.SetRowValue("INDEX(A2:E500," + k.ToString() + ",1)", new List<object> { item.linkid, name1, name2 });
                item_bak = item;
                k++;
            }*/

            //第三个sheet
            ea.SetCurrentSheet(3);

            DataTable dt = new DataTable("FileLinks");
            dt.Columns.Add("CategoryId", System.Type.GetType("System.String"));
            dt.Columns.Add("FileName", System.Type.GetType("System.String"));
            dt.Columns.Add("FileDescription", System.Type.GetType("System.String"));
            dt.Columns.Add("FileType", System.Type.GetType("System.String"));
            dt.Columns.Add("ReleaseDate", System.Type.GetType("System.String"));
            dt.Columns.Add("UpdateDate", System.Type.GetType("System.String"));
            dt.Columns.Add("Url", System.Type.GetType("System.String"));

            foreach (FileLink lnk in FileLinks)
            {
                if (lnk.IsFileLink)
                    dt.Rows.Add(new object[] { lnk.CategoryPath, lnk.Name, lnk.Description, lnk.FileType,lnk.ReleaseDate,"",lnk.Url });
            }

            if (dt.Rows.Count > 0)
                ea.SetRangeValues(dt, false, "A2");
            ea.SaveAs(path);

            ea.Quit();
            UpdateMsg(this.GetType().Name, "已完成", ref f);
        }
    }

    /// <summary>
    /// 网页链接类
    /// </summary>
    public class WebLink
    {
        /// <summary>
        /// 链接层次，从0开始
        /// </summary>
        public int LinkLevel;
        /// <summary>
        /// 链接ID
        /// </summary>
        public int LinkId;

        /// <summary>
        /// 父节点连接ID
        /// </summary>
        public int ParentLinkId;
        /// <summary>
        /// url
        /// </summary>
        public string LinkUrl;        
        /// <summary>
        /// 链接名称
        /// </summary>
        public string LinkName;
        /// <summary>
        /// 描述
        /// </summary>
        public string LinkDescription;
        /// <summary>
        /// 类型，当链接文件是表示文件后缀名
        /// </summary>
        public string LinkType;
        /// <summary>
        /// 连接是否文件
        /// </summary>
        public bool IsFile;
        /// <summary>
        /// 下一层的HtmlAgilityPack路径，最后层级时为空
        /// </summary>
        public string XPath = string.Empty;
        public string NameRelaPath = string.Empty;
        public string UrlRelaPath = string.Empty;
        public string DescRelaPath = string.Empty;

        public WebLink NewSubLink(int id,string url, string name, string description,
            string xpath,
            string nameRelaPath = "",
            string urlRelaPath = "",
            string descRelaPath = "")
        {
            WebLink wl = new WebLink();
            wl.LinkId = id;
            wl.LinkLevel = this.LinkLevel + 1;
            wl.ParentLinkId = this.LinkId;
            wl.LinkUrl = url;
            wl.LinkName = name;
            wl.LinkDescription = description;
            wl.XPath = xpath;
            wl.NameRelaPath = nameRelaPath;
            wl.UrlRelaPath = urlRelaPath;
            wl.DescRelaPath = descRelaPath;

            int i = wl.LinkUrl.LastIndexOf(".");
            if (i > 0)
                wl.LinkType = wl.LinkUrl.Substring(i+1).ToUpper();

            return wl;
        }

        public static String FormatUrl(String urlRelative, string urlAbsolute)
        {

            Uri baseUri = new Uri(urlAbsolute); // http://www.enet.com.cn/enews/inforcenter/designmore.jsp
            Uri absoluteUri = new Uri(baseUri, urlRelative);//相对绝对路径都在这里转 这里的urlx ="../test.html"
            return absoluteUri.ToString();//   http://www.enet.com.cn/enews/test.html   
        }
    }

    class FileLinksScraper
    {
        //主页URL
        public string MainPage = "";
        //要抓取的文件链接所在层级
        public int FileLevel = 0;
        //厂商名称
        public string CompanyName = string.Empty;
        //栏目名称
        public string ColumnName = string.Empty;
        //栏目地址
        public string CompanyUrl = string.Empty;

        public List<WebLink> WebLinks = new List<WebLink> { };

        public delegate void OnProcessing(string className, string msg, ref bool bCancel);
        public event OnProcessing OnProcessingEvent = null;

        /// <summary>
        /// 允许继承类改变weblink内容
        /// </summary>
        /// <param name="wl">weblink</param>
        /// <param name="sublingIndex">兄弟节点的索引,从0开始</param>
        public delegate void OnReviseXPath(ref WebLink wl,int sublingIndex);
        public event OnReviseXPath ReviseXPath = null;        

        ~FileLinksScraper()
        {
        }

        protected void UpdateMsg(string className, string msg,ref bool bCancel)
        {
            if (OnProcessingEvent != null)
                OnProcessingEvent(className, msg,ref bCancel);
        }

        public virtual void GenerateNextLevelLinks(HtmlAgilityPack.HtmlDocument doc,WebLink wlParent,
            string xPath,
            string nameRelaPath = "",
            string urlRelaPath = "",
            string DescRelaPath = "")
        {
            int k = 0;
            foreach (HtmlAgilityPack.HtmlNode node in doc.DocumentNode.SelectNodes(wlParent.XPath))
            {
                WebLink wl = new WebLink();
                wl.LinkLevel = wlParent.LinkLevel + 1;
                wl.LinkId = WebLinks.Count;
                wl.ParentLinkId = wlParent.LinkId;

                string urlPath = wlParent.UrlRelaPath;
                string namePath = wlParent.NameRelaPath;
                string descPath = wlParent.DescRelaPath;
              
                HtmlNode hn;
                if (!string.IsNullOrEmpty(urlPath))
                {
                    hn = node.SelectSingleNode(urlPath);
                    if (hn != null)
                        try
                        {
                            wl.LinkUrl = node.SelectSingleNode(urlPath).GetAttributeValue("href", "");
                            if (string.IsNullOrEmpty(wl.LinkUrl))
                                wl.LinkUrl = wlParent.LinkUrl;
                        }
                        catch (Exception)
                        {
                            wl.LinkUrl = "";
                        }

                    //if (string.IsNullOrEmpty(wl.LinkUrl))
                    //    continue;
                }
                else
                {
                    try
                    {
                        wl.LinkUrl = node.GetAttributeValue("href", "");
                        if (string.IsNullOrEmpty(wl.LinkUrl))
                            wl.LinkUrl = wlParent.LinkUrl;
                    }
                    catch (Exception)
                    {
                        wl.LinkUrl = "";
                    }
                }

                wl.LinkUrl = WebLink.FormatUrl(wl.LinkUrl, wlParent.LinkUrl);

                if (!string.IsNullOrEmpty(namePath))
                {
                    hn = node.SelectSingleNode(namePath);
                    if (hn != null)
                        wl.LinkName = hn.InnerText;
                    else
                        continue;
                }
                else
                    wl.LinkName = node.InnerText;

                if (!string.IsNullOrEmpty(descPath))
                {
                    hn = node.SelectSingleNode(descPath);
                    if (hn != null)
                        wl.LinkDescription = hn.InnerText;
                    //else
                    //    continue;
                }
                wl.XPath = xPath;
                wl.NameRelaPath = nameRelaPath;
                wl.UrlRelaPath = urlRelaPath;
                wl.DescRelaPath = DescRelaPath;

                if (wl.LinkUrl != null)
                {
                    int i = wl.LinkUrl.LastIndexOf(".");
                    if (i > 0 && wl.LinkLevel == FileLevel)
                        wl.LinkType = wl.LinkUrl.Substring(i + 1);
                }

                //是否需要根据link内容，更改xpath属性值
                if (ReviseXPath != null)
                    ReviseXPath(ref wl,k);
                k++;

                WebLinks.Add(wl);
            }
        }     

        public virtual void GenerateAllLinks()
        {
            return;
        }

        /// <summary>
        /// 从表格[row,col]开始，显示wls内的所有节点
        /// </summary>
        /// <param name="ea"></param>
        /// <param name="row">代表与原点行号的距离，原点为1</param>
        /// <param name="col">代表与原点列号的距离，原点为1，同时代表层级</param>
        public virtual void ColumnHierachy(ExcelApp ea,IEnumerable<WebLink> wls,ref int row,int col)
        {
            if (col == FileLevel - 1) //到了倒数第2层，即目录架构最低层
            {
                foreach (WebLink wl in wls)
                {
                    ea.SetCellValue("INDEX(A2:F500," + row.ToString() + ",1)", wl.LinkId);
                    ea.SetCellValue("INDEX(B2:F500," + row.ToString() + "," + col.ToString() + ")" , wl.LinkName);
                    row++;
                }
            }
            else //递归
            {
                foreach (WebLink wl in wls)
                {
                    ea.SetCellValue("INDEX(B2:F500," + row.ToString() + "," + col.ToString() + ")", wl.LinkName);

                    IEnumerable<WebLink> wls_children = WebLinks.Where(v1 => v1.LinkLevel == wl.LinkLevel + 1 && v1.ParentLinkId == wl.LinkId).OrderBy(v1 => v1.LinkId);

                    ColumnHierachy(ea, wls_children, ref row, col+1);
                }
            }
        }

        public virtual void ExportToExcel(ExcelApp ea,string path,string template)
        {
            bool f = false;
            UpdateMsg(this.GetType().Name, "正在导出到EXCEL到" + path, ref f);

            ea.OpenFile(template);

            //第一个sheet
            ea.SetCurrentSheet(1);
            ea.SetCellValue("A2", CompanyName);
            ea.SetCellValue("B2", ColumnName);
            ea.SetCellValue("C2", MainPage);

            //第二个sheet，显示栏目树型结构
            ea.SetCurrentSheet(2);

            IEnumerable<WebLink> wls = WebLinks.Where(v1 => v1.LinkLevel == 1).OrderBy(v1 => v1.LinkId);

            int row = 1;
            ColumnHierachy(ea, wls, ref row, 1);
            /*var q = from v1 in WebLinks.Where(s=>s.LinkLevel==1)
                    from v2 in WebLinks.Where(s=>s.LinkLevel==2)
                    where v1.LinkId == v2.ParentLinkId 
                    orderby v1.LinkId ascending, v2.LinkId ascending
                    select new {linkid = v2.LinkId.ToString(),n1 = v1.LinkName,n2 = v2.LinkName};

            int k = 1;
            var item_bak = q.ElementAt(0);
            foreach(var item in q)
            {
                string name1 = item.n1, name2 = item.n2;
                if (k > 1)
                {
                    if (item.n1 == item_bak.n1)
                        name1 = "";
                    if (item.n2 == item_bak.n2)
                        name2 = "";
                }
                ea.SetRowValue("INDEX(A2:E500," + k.ToString() + ",1)", new List<object> { item.linkid, name1, name2 });
                item_bak = item;
                k++;
            }*/

            //第三个sheet
            ea.SetCurrentSheet(3);

            DataTable dt = new DataTable("FileLinks");
            dt.Columns.Add("ColumnId", System.Type.GetType("System.String"));
            dt.Columns.Add("FileName", System.Type.GetType("System.String"));
            dt.Columns.Add("FileType", System.Type.GetType("System.String"));
            dt.Columns.Add("LinkUrl", System.Type.GetType("System.String"));

            foreach (WebLink wl in WebLinks)
            {
                if (wl.LinkLevel == 3)
                    dt.Rows.Add(new object[] { wl.ParentLinkId.ToString(),wl.LinkName,wl.LinkType,wl.LinkUrl });
            }

            if (dt.Rows.Count > 0)
                ea.SetRangeValues(dt, false, "A2");
            ea.SaveAs(path);

            ea.Quit();
            UpdateMsg(this.GetType().Name, "已完成", ref f);
        }

        public virtual void GenerateNextLevelLinks2(HtmlAgilityPack.HtmlDocument doc, WebLink wlParent,
            string xPath,
            string nameRelaPath = "",
            string urlRelaPath = "",
            string DescRelaPath = "",
            bool isFile = false)
        {
            int k = 0;
            foreach (HtmlAgilityPack.HtmlNode node in doc.DocumentNode.SelectNodes(xPath))
            {
                WebLink wl = new WebLink();
                wl.LinkLevel = wlParent.LinkLevel + 1;
                wl.LinkId = WebLinks.Count;
                wl.ParentLinkId = wlParent.LinkId;
                wl.IsFile = isFile;

                HtmlNode hn;

                if (!string.IsNullOrEmpty(urlRelaPath))
                {
                    hn = node.SelectSingleNode(urlRelaPath);
                    if (hn != null)
                        try
                        {
                            wl.LinkUrl = node.SelectSingleNode(urlRelaPath).GetAttributeValue("href", "");
                            if (string.IsNullOrEmpty(wl.LinkUrl))
                                wl.LinkUrl = wlParent.LinkUrl;
                        }
                        catch (Exception)
                        {
                            wl.LinkUrl = "";
                        }

                    //if (string.IsNullOrEmpty(wl.LinkUrl))
                    //    continue;
                }
                else
                {
                    try
                    {
                        wl.LinkUrl = node.GetAttributeValue("href", "");
                        if (string.IsNullOrEmpty(wl.LinkUrl))
                            wl.LinkUrl = wlParent.LinkUrl;
                    }
                    catch (Exception)
                    {
                        wl.LinkUrl = "";
                    }
                }

                wl.LinkUrl = WebLink.FormatUrl(wl.LinkUrl, wlParent.LinkUrl);

                if (!string.IsNullOrEmpty(nameRelaPath))
                {
                    hn = node.SelectSingleNode(nameRelaPath);
                    if (hn != null)
                        wl.LinkName = hn.InnerText;
                    else
                        continue;
                }
                else
                    wl.LinkName = node.InnerText;

                if (!string.IsNullOrEmpty(DescRelaPath))
                {
                    hn = node.SelectSingleNode(DescRelaPath);
                    if (hn != null)
                        wl.LinkDescription = hn.InnerText;
                    //else
                    //    continue;
                }
                //wl.XPath = xPath;
                //wl.NameRelaPath = nameRelaPath;
                //wl.UrlRelaPath = urlRelaPath;
                //wl.DescRelaPath = DescRelaPath;

                if (wl.LinkUrl != null)
                {
                    int i = wl.LinkUrl.LastIndexOf(".");
                    if (i > 0 && wl.LinkLevel == FileLevel)
                        wl.LinkType = wl.LinkUrl.Substring(i + 1);
                }

                //是否需要根据link内容，更改xpath属性值
                if (ReviseXPath != null)
                    ReviseXPath(ref wl, k);
                k++;

                WebLinks.Add(wl);
            }
        }        

        public virtual void GenerateSubWebLinks(HtmlAgilityPack.HtmlDocument doc, WebLink wlParent, string xPath)
        {
            int k = 0;
            foreach (HtmlAgilityPack.HtmlNode node in doc.DocumentNode.SelectNodes(xPath))
            {
                WebLink wl = new WebLink();
                wl.LinkLevel = wlParent.LinkLevel + 1;
                wl.LinkId = WebLinks.Count;
                wl.ParentLinkId = wlParent.LinkId;

                try
                {
                    wl.LinkUrl = node.GetAttributeValue("href", "");
                    if (string.IsNullOrEmpty(wl.LinkUrl))
                        wl.LinkUrl = wlParent.LinkUrl;
                }
                catch (Exception)
                {
                    wl.LinkUrl = "";
                }

                wl.LinkName = node.InnerText;

                wl.LinkUrl = WebLink.FormatUrl(wl.LinkUrl, wlParent.LinkUrl);

                //wl.XPath = xPath;
                //wl.NameRelaPath = nameRelaPath;
                //wl.UrlRelaPath = urlRelaPath;
                //wl.DescRelaPath = DescRelaPath;

                if (wl.LinkUrl != null)
                {
                    int i = wl.LinkUrl.LastIndexOf(".");
                    if (i > 0 && wl.LinkLevel == FileLevel)
                        wl.LinkType = wl.LinkUrl.Substring(i + 1);
                }

                //是否需要根据link内容，更改xpath属性值
                if (ReviseXPath != null)
                    ReviseXPath(ref wl, k);

                k++;

                WebLinks.Add(wl);
            }
        }

        public virtual void GenerateNextTwoLevelLinks(HtmlAgilityPack.HtmlDocument doc, WebLink wlParent,
            string xPathBase, //xpath根路径
            string xPath1, //下一层目录相对路径
            string xPath2  //再下一层
        )
        {
            int k = 0;
            foreach (HtmlAgilityPack.HtmlNode node in doc.DocumentNode.SelectNodes(xPathBase))
            {
                WebLink wl = new WebLink();
                wl.LinkLevel = wlParent.LinkLevel + 1;
                wl.LinkId = WebLinks.Count;
                wl.ParentLinkId = wlParent.LinkId;

                HtmlAgilityPack.HtmlNode node1 = node.SelectSingleNode(xPath1);

                try
                {
                    wl.LinkUrl = node1.GetAttributeValue("href", "");
                    if (string.IsNullOrEmpty(wl.LinkUrl))
                        wl.LinkUrl = wlParent.LinkUrl;
                }
                catch (Exception)
                {
                    wl.LinkUrl = "";
                }

                wl.LinkName = node1.InnerText;

                wl.LinkUrl = WebLink.FormatUrl(wl.LinkUrl, wlParent.LinkUrl);

                //wl.XPath = xPath;
                //wl.NameRelaPath = nameRelaPath;
                //wl.UrlRelaPath = urlRelaPath;
                //wl.DescRelaPath = DescRelaPath;

                if (wl.LinkUrl != null)
                {
                    int i = wl.LinkUrl.LastIndexOf(".");
                    if (i > 0 && wl.LinkLevel == FileLevel)
                        wl.LinkType = wl.LinkUrl.Substring(i + 1);
                }

                //是否需要根据link内容，更改xpath属性值
                if (ReviseXPath != null)
                    ReviseXPath(ref wl, k);

                k++;

                WebLinks.Add(wl);

                //同一页内找下一级的目录
                if (!string.IsNullOrEmpty(xPath2))
                {
                    HtmlNodeCollection hnc = node.SelectNodes(xPath2);
                    if (hnc != null)
                    foreach (HtmlAgilityPack.HtmlNode node2 in hnc)
                    {
                        WebLink wl2 = new WebLink();
                        wl2.LinkLevel = wlParent.LinkLevel + 2;
                        wl2.LinkId = WebLinks.Count;
                        wl2.ParentLinkId = wl.LinkId;

                        try
                        {
                            wl2.LinkUrl = node2.GetAttributeValue("href", "");
                            if (string.IsNullOrEmpty(wl2.LinkUrl))
                                wl2.LinkUrl = wl.LinkUrl;
                        }
                        catch (Exception)
                        {
                            wl2.LinkUrl = "";
                        }

                        wl2.LinkUrl = WebLink.FormatUrl(wl2.LinkUrl, wl.LinkUrl);

                        wl2.LinkName = node2.InnerText;

                        WebLinks.Add(wl2);
                    }
                }
            }
        }
    }
}
